﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.Ajax
{
    public class LogCommentDeleteAjax:YouHP.Pages.BasePage
    {

        protected List<LogCommentInfo> LogCommentDataList;
        protected LogTopicInfo LogData;
        protected int PageIndex;
        protected int PageSize = 15;
        protected int DataCount = 0;
        protected int PageCount;
        protected string PagingString;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            //判断条件
            int lid = RequestTools.GetInt("lid", 0);
            int logCommentId = RequestTools.GetInt("logCommentId", 0);
            if (lid <= 0) return;
            if (logCommentId <= 0) return;

            LogData = LogTopicBLL.Instance.GetLogTopicInfo(lid);
            if (LogData.Id <= 0) return;


            //执行删除
            LogCommentBLL.Instance.DeleteLogComment(lid, logCommentId);

            //生成返回XML
            PageIndex = RequestTools.GetQueryInt("page", 1);
            LogCommentDataList = LogCommentBLL.Instance.GetLogCommentList(PageSize, PageIndex, LogData.Id, out DataCount);
            PageCount = Utils.GetPageCount(DataCount, PageSize);

            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pl_title\">" + "<span style=\"float:left;\">对该日志的评论</span>" + "<span style=\"float:right;\">" + "共<b><font name=\"fedcount\" id=\"fedcount\">" + DataCount + "</font></b>条</span></div>");
            sb.Append("<ul class=\"hugbook\" id=\"hugbook\">");

            foreach (LogCommentInfo item in LogCommentDataList)
            {

                sb.Append("<li><div style=\"padding-left:20px; text-align:center; float:left;\">");
                string TempUrl = "/Profile/Index.aspx?uid=" + item.PostUserId;
                sb.Append("<a href=\"" + this.GetUrl(TempUrl) + "\"><img class=\"avatar\" width=\"45\" height=\"45\" src=\"" + UserBLL.Instance.GetUserInfo(item.PostUserId).Avatar + "\"/>");
                sb.Append("</a></div>");
                sb.Append("<div style=\"margin:0pt 10px 0pt 82px;\"><p class=\"graytext\"><span style=\"float:right;\">");
                if (UserData.Id > 0)
                {
                    if (UserData.Id != item.PostUserId && UserData.Id == LogData.HostUserId)
                    {

                        sb.Append("<a href=\"javascript:replyLogComment(\'" + item.Id + "\');\"><font color=\"#949494\">回复</font></a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }

                    if (item.PostUserId == UserData.Id || LogData.HostUserId == UserData.Id)
                    {

                        sb.Append("<a href=\"javascript:if(window.confirm(\'确定删除该评论和其所有回复？\')) LogCommentDelete(" + item.Id + ");\"><font color=\"#949494\">删除</font></a>&nbsp;&nbsp;&nbsp;&nbsp;");
                    }
                }
                TempUrl = "/Profile/Index.aspx?uid=" + item.PostUserId;
                sb.Append("</span><a class=\"client_name\" href=\"" + this.GetUrl(TempUrl) + "\">" + item.PostUserName + "</a>");
                TempUrl = item.PostTime.ToString("yyyy-MM-dd  hh:mm");
                sb.Append("&nbsp;" + TempUrl + "</p><p class=\"longtextfix\">" + item.Body + "</p></div>");
                sb.Append("<div style=\"clear: both;\"/>");

                //评论回复列表
                List<LogCommentInfo> LogCommentReplyList = LogCommentBLL.Instance.GetLogCommentReplyList(item.Id);
                if (LogCommentReplyList.Count != 0)
                {

                    sb.Append("<div style=\"width:500px;margin-left:80px;padding:5px; border-left:1px solid #E1E4E6;border-top:1px solid #E1E4E6;\">");
                    foreach (LogCommentInfo replyItem in LogCommentReplyList)
                    {
                        sb.Append("<div style=\"margin-bottom:5px;\"><div style=\"text-align:center; float:left;\">");
                        string TempUrl2 = "/Profile/Index.aspx?uid=" + replyItem.PostUserId;
                        sb.Append("<a href=\"" + this.GetUrl(TempUrl2) + "\"><img width=\"25\" height=\"25\" src=\"" + UserBLL.Instance.GetUserInfo(replyItem.PostUserId).Avatar + "\"/>");
                        sb.Append("</a></div>");
                        sb.Append("<div style=\"margin:0pt 10px 0pt 30px;\">");
                        sb.Append("<div>");
                        sb.Append("<span style=\"float:right;\">" + replyItem.PostTime.ToString("yyyy-MM-dd  HH:mm") + "</span>");
                        sb.Append("<span><a class=\"client_name\" href=\"" + this.GetUrl("/Profile/Index.aspx?uid=" + replyItem.PostUserId) + "\">" + replyItem.PostUserName + "</a>&nbsp;回复到：</span>");
                        sb.Append("</div>");
                        sb.Append("<div class=\"longtextfix\">" + replyItem.Body + "</div>");

                        sb.Append("</div>");
                        sb.Append("</div>");
                    }

                    if (UserData.Id == LogData.HostUserId || UserData.Id == item.PostUserId)
                    {
                        sb.Append("<div><a  href=\"javascript:replyLogComment(" + item.Id + ");\"><font color=\"#949494\">回复</font></a>&nbsp;&nbsp;<a href=\"javascript:if(window.confirm(\'确定删除该评论和其所有回复？\')) LogCommentDelete(" + item.Id + ");\"><font color=\"#949494\">删除</font></a></div>");
                    }
                  //隐藏的回复框
                    sb.Append("<div id=\"replybox_" + item.Id + "\" style=\"display:none;margin-top:10px;\"><div style=\"float:left; width:40px; color:#949494;\">回复：</div>");
                    sb.Append("<textarea name=\"replycontent_" + item.Id + "\" id=\"replycontent_" + item.Id + "\" width=\"300px\" height=\"80px\" cols=\"60\" rows=\"6\"></textarea>");
                    sb.Append("<p style=\"padding-left:200px\"><input id=\"reply_submit_" + item.Id + "\"  type=\"button\" value=\"回复\" onclick=\"LogCommentReplyAdd(" + LogData.Id + "," + item.Id +");\">&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"取消\" onclick=\"replyCancell(" + item.Id + ");\"></div>");

                    sb.Append("</div>");
                }
                else
                {
                    //隐藏的回复框
                    sb.Append("<div id=\"replybox_" + item.Id + "\" style=\"display:none;margin-left:80px;margin-top:20px;\"><div style=\"float:left; width:40px; color:#949494;\">回复：</div>");
                    sb.Append("<textarea name=\"replycontent_" + item.Id + "\" id=\"replycontent_" + item.Id + "\" width=\"300px\" height=\"80px\" cols=\"60\" rows=\"6\"></textarea>");
                    sb.Append("<p style=\"padding-left:200px\"><input id=\"reply_submit_" + item.Id + "\"  type=\"button\" value=\"回复\" onclick=\"LogCommentReplyAdd(" + LogData.Id + "," + item.Id +");\">&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"取消\" onclick=\"replyCancell(" + item.Id + ");\"></div>");
                }
                sb.Append("</li>");

            }
            sb.Append("</ul>");

            PagingString = this.GetPagingString2(PageIndex, PageCount, "", "javascript:ViewLogCommentPage");
            sb.Append("<div class=\"pages\" style=\"padding:10px 5px;\">" + PagingString + "</div>");


            ResponseXML(sb);

        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}

